<?php
/**
 * 日期字符串
 * Enter description here ...
 */
function  getFormatDate(){
	date_default_timezone_set("Etc/GMT-8"); 
	$my_t=getdate();
	$NOW_DATE =  $my_t[year]  . $my_t[mon] . $my_t[mday] ;
	return $NOW_DATE;
}
/**
 * 当前月份
 * Enter description here ...
 */
function getFileMonth(){
	date_default_timezone_set("Etc/GMT-8"); 
	$my_t=getdate();
	$NOW_Month =  $my_t[year] ."-" . $my_t[mon]  ;
	return $NOW_Month;
}
/**
 * 带有横杆的日期
 * Enter description here ...
 */
function  getFormatDate_underline(){
	date_default_timezone_set("Etc/GMT-8"); 
	$my_t=getdate();
	$NOW_DATE =  $my_t[year] ."-" . $my_t[mon] ."-".$my_t[mday] . " " .$my_t[hours].":".$my_t[minutes].":".$my_t[seconds];
	return $NOW_DATE;
}

/**
 * 创建文件夹
 * Enter description here ...
 * @param unknown_type $path
 */
function mkTXTDir($path){
	if (! file_exists ( $path )) {
		@mkdir ( $path, 0777 , true); //不存在则创建;
	}
}

/**
 * 去除后缀名
 * Enter description here ...
 * @param unknown_type $name
 */
function getFilenameNoExt($name){
	$p = strrpos($name,'.');     //得到最后一个点的位置
	return strtolower(substr($name,0,$p));
}
/**
 * 去除字符串前面的0
 * Enter description here ...
 * @param unknown_type $name
 */
function getstrNoZero($name){
	return intval($name)."";
}

/**
 * 上传zip生成txt文件 并且转化横版字符   分割zip文件名
 * Enter description here ...
 * @param unknown_type $files
 */
function uploadBook($error,$tmpname,$name,$fsize){
	$baseDir = INCLUDE_PATH . "upload";//上传文件根目录
	$baseTxt_Dir =  "TXT_FILE";//小说根目录
	$extention_list = "7z|zip|rar";//文件类型
	$fileSize = 5;//文件大小限制1M
	$TXT_dir="";//本篇小说的目录
	$txt_name = "";
	$txtMAP = array();
	$go = new Trans;  //繁体转化类
//	echo "<script language=javascript>alert('$name');</script>";
	if ($error > 0) {
		echo "Return Code: " . $error . "<br />";
	} else {
		$up = new upload_file ( $tmpname, $name, $fsize, $baseDir, $extention_list, $fileSize );
		//$up->set_is_default_filename ( true ); //用原始文件名
		//$up->set_have_subdirectory ( true, "12" ); //创建子目录 目录名12
		$up->upload ();
//		echo $up->file_path."---";
//		echo $up->file_last_name." <br/>";
		$txt_name = $name;
		$NOW_DATE = getFormatDate();
		$now_month = getFileMonth();
		$TXT_dir = $baseTxt_Dir ."/".$now_month."/".uniqid ($NOW_DATE);
		mkTXTDir(INCLUDE_PATH.$TXT_dir);//创建目录
		if(file_exists($up->file_path))
			{
				$zip=zip_open(realpath($up->file_path));
			    if (is_resource($zip)) {
			        while ($zip_entry = zip_read($zip)) {
			        	$txtName = zip_entry_name($zip_entry);
			            if (zip_entry_open($zip, $zip_entry, "r")) {
			                $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
							$buf = mb_convert_encoding($buf, "UTF-8", "UCS-2");//转化编码
							/**
							 * 转化横版字符
							 * Enter description here ...
							 * @var unknown_type
							
							$buf = str_replace("﹁", "「", $buf);
							$buf = str_replace("︽", "《", $buf);
							$buf = str_replace("︾", "》", $buf);
							$buf = str_replace("﹂", "」", $buf);
							$buf = str_replace("︵", "（", $buf);
							$buf = str_replace("︶", "）", $buf);
							$buf = str_replace("﹃", "『", $buf);
							$buf = str_replace("﹄", "』", $buf);
							 */
							
							 
							 
							
							 
							///繁体 转换 简体
							$buf = $go->t2c($buf);  
							//转化文件名称 去除前面的0
							$newtxtname = getFilenameNoExt($txtName);
							$newtxtname = getstrNoZero($newtxtname);
							$newtxtname = $newtxtname.".txt";
							file_put_contents(INCLUDE_PATH.$TXT_dir."/".$newtxtname, $buf);//写入文件
			
			                zip_entry_close($zip_entry);
			            }
			
			        }
			        zip_close($zip);
			    }else{
			        echo "can not open the file: test.zip";   
			    }
			    
			    //解析完成 删除zip文件
			    unlink($up->file_path);
			}else{
			    echo "can not find the file: test.zip";
			}
		
	}
	if ($TXT_dir!=""){
		$TXT_dir=str_replace('\\','/',$TXT_dir);
	}
	if ($txt_name!=""){
		$tmp1 = explode(".zip",$txt_name);
		$tmp2 = explode("-",$tmp1[0]);
	}
	$txtMAP[0] = $tmp2[0];//作者名
	$txtMAP[1] = $tmp2[1];//文章名
	$txtMAP[2] = $TXT_dir;//路径
	return $txtMAP;
	
}

/**
 * 上传zip生成txt文件 并且转化横版字符
 * Enter description here ...
 * @param unknown_type $files
 */
function uploadBook2($error,$tmpname,$name,$fsize,$isUTF8,$isBIG5){
	$baseDir = INCLUDE_PATH . "upload";//上传文件根目录
	$baseTxt_Dir =  "TXT_FILE";//小说根目录
	$extention_list = "7z|zip|rar";//文件类型
	$fileSize = 5;//文件大小限制1M
	$TXT_dir="";//本篇小说的目录
	$go = new Trans;  //繁体转化类
//	echo "<script language=javascript>alert('$name');</script>";
	if ($error > 0) {
		echo "Return Code: " . $error . "<br />";
	} else {
		$up = new upload_file ( $tmpname, $name, $fsize, $baseDir, $extention_list, $fileSize );
		//$up->set_is_default_filename ( true ); //用原始文件名
		//$up->set_have_subdirectory ( true, "12" ); //创建子目录 目录名12
		$up->upload ();
//		echo $up->file_path."---";
//		echo $up->file_last_name." <br/>";
		$NOW_DATE = getFormatDate();
		$now_month = getFileMonth();
		$TXT_dir = $baseTxt_Dir ."/".$now_month."/".uniqid ($NOW_DATE);
		mkTXTDir(INCLUDE_PATH.$TXT_dir);//创建目录
		if(file_exists($up->file_path))
			{
				$zip=zip_open(realpath($up->file_path));
			    if (is_resource($zip)) {
			        while ($zip_entry = zip_read($zip)) {
			        	$txtName = zip_entry_name($zip_entry);
			            if (zip_entry_open($zip, $zip_entry, "r")) {
			                $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
			                if($isUTF8){
			                	$buf = mb_convert_encoding($buf, "UTF-8", "UCS-2");//转化编码
			                }else{
			                	$buf = mb_convert_encoding($buf, "UTF-8", "GB2312");//转化编码
			                }
							/**
							 * 转化横版字符
							 * Enter description here ...
							 * @var unknown_type
							
							$buf = str_replace("﹁", "「", $buf);
							$buf = str_replace("︽", "《", $buf);
							$buf = str_replace("︾", "》", $buf);
							$buf = str_replace("﹂", "」", $buf);
							$buf = str_replace("︵", "（", $buf);
							$buf = str_replace("︶", "）", $buf);
							$buf = str_replace("﹃", "『", $buf);
							$buf = str_replace("﹄", "』", $buf);
							 */
							
							 
							 
							
							 if ($isBIG5){
							 	///繁体 转换 简体
								$buf = $go->t2c($buf);  
							 }
							 //转化文件名称 去除前面的0
							$newtxtname = getFilenameNoExt($txtName);
							$newtxtname = getstrNoZero($newtxtname);
							$newtxtname = $newtxtname.".txt";
							file_put_contents(INCLUDE_PATH.$TXT_dir."/".$newtxtname, $buf);//写入文件
			
			                zip_entry_close($zip_entry);
			            }
			
			        }
			        zip_close($zip);
			    }else{
			        echo "上传的不是zip资源文件";   
			    }
			    
			    //解析完成 删除zip文件
			    unlink($up->file_path);
			}else{
			    echo "上传zip文件找不到";
			}
		
	}
	if ($TXT_dir!=""){
		$TXT_dir=str_replace('\\','/',$TXT_dir);
	}
	return $TXT_dir;
	
}


/**
 * 上传图片 并压缩
 * Enter description here ...
 * @param unknown_type $error
 * @param unknown_type $tmpname
 * @param unknown_type $name
 * @param unknown_type $fsize
 */
function uploadImage($error,$tmpname,$name,$fsize){
	$base_width  =  150;
	$base_height = 200;
	$baseDir = INCLUDE_PATH . "IMAGE_FILE";//图片上传目录
	$min_dir =  "/min";//压缩图片目录
	$extention_list = "jpg|gif|png|jpeg";//文件类型
	$fileSize = 5;//文件大小限制1M
	$return_dir="";//输出
	if ($error > 0) {
		echo "Return Code: " . $error . "<br />";
	} else {
		$up = new upload_file ( $tmpname, $name, $fsize, $baseDir, $extention_list, $fileSize );
		$up->upload ();
		
		$imgurl = $up->file_path;//图片路径
		//开始压缩图片
		$img=getimagesize($imgurl);//获取图片信息 $img[0]是获得图片的宽度，$img[1]是获得图片的高度
		$img_width = $img[0];
		$img_height = $img[1];
		switch ($img[2])//判断图片的格式  类型的标记：1 = GIF，2 = JPG，3 = PNG，4 = SWF，5 = PSD，6 = BMP，7 = TIFF(intel byte order)，8 = TIFF(motorola byte order)，9 = JPC，10 = JP2，11 = JPX，12 = JB2，13 = SWC，14 = IFF，15 = WBMP，16 = XBM
		{
			case 1:
			  $im=imagecreatefromgif($imgurl);
			  break;
			case 2:
			  $im=imagecreatefromjpeg($imgurl);
			  break;
			case 3:
			  $im=imagecreatefrompng($imgurl);
			  break;
		}
//		$ratioW = $base_width/$img_width;
//		$ratioH = $base_height/$img_height;
//		if ($ratioH < $ratioW){
//			$ratio = $ratioH;
//		}else{
//			$ratio = $ratioW;
//		}
//		//计算新图片长宽
//		$newwidth = $img_width * $ratio;    
//        $newheight = $img_height * $ratio;    
		$imnew = ImageCreateTrueColor($base_width, $base_height);//定死的宽 长
		imagecopyresized($imnew, $im, 0, 0, 0, 0, $base_width, $base_height, $img_width, $img_height);
		//输出路径
		$minPath = $baseDir.$min_dir."/".$up->file_last_name;
		ImageJpeg ($imnew,$minPath);    
        ImageDestroy ($imnew);    
		
		$return_dir = $up->file_last_name;
	}
	
	return $return_dir;
}



?>